////////////////////////////////////////////////////////////////////////////////
//
//  FLEX USER GROUP HOKURIKU
//  Copyright 2009 Flex User Group Hokuriku
//  All Rights Reserved.
// 
//  NOTICE: Flex User Group Hokuriku permits you to use, 
//  modify, and distribute this file 
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package net.fxug.hokuriku.chocolat.view
{
    import flash.display.NativeWindow;
    import flash.display.NativeWindowInitOptions;
    import flash.display.NativeWindowSystemChrome;
    import flash.display.NativeWindowType;
    import flash.display.Screen;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.text.TextFormat;
    
    import mx.logging.ILogger;
    
    import net.fxug.hokuriku.chocolat.events.SessionTimerEvent;
    import net.fxug.hokuriku.chocolat.helper.TimeFormatter;
    import net.fxug.hokuriku.chocolat.model.SessionTimer;
    import net.fxug.hokuriku.chocolat.utils.LoggingUtil;

    /**
     * タイマーのタイムライン表示クラス
     * タイムアップ、またはタイムラインをダブルクリックすると閉じる
     *
     * @author shoito
     */
    public class TimelineTimer extends NativeWindow
    {
        private static const WINDOW_HEIGHT:Number = 84;
        private static const BAR_HEIGHT:Number = 20;
        private static const BAR_ROUND:Number = 5;
        private static const BASETIME_COLOR:uint = 0x0000ff;
        private static const BONUSTIME_COLOR:uint = 0x00ff00;
        private static const GUTTER:Number = 10;
        private static const ALARM_TIME:int = 15;

        /** ロガー */
        private static var logger:ILogger = LoggingUtil.getLogger(TimelineTimer);

        private var remainingTimeFormat:TextFormat = new TextFormat(null, 48, 0xff0000, true);
        private var periodFormat:TextFormat = new TextFormat(null, 16, 0x333333, true);
		private var timeFormatter:TimeFormatter = new TimeFormatter(true);

        private var sessionTimer:SessionTimer;

        private var timeline:Sprite;
        private var remainingTime:TextField;
        private var time:TextField;

        public function TimelineTimer()
        {
            this.sessionTimer = SessionTimer.getInstance();

            var options:NativeWindowInitOptions = new NativeWindowInitOptions();
            options.type = NativeWindowType.LIGHTWEIGHT;
            options.systemChrome = NativeWindowSystemChrome.NONE;
            options.transparent = true;
            super(options);

            initializeContents();
        }

        private function initializeContents():void
        {
            this.stage.align = StageAlign.TOP_LEFT;
            this.stage.scaleMode = StageScaleMode.NO_SCALE;

            this.alwaysInFront = true;
            this.width = Screen.mainScreen.bounds.width;
            this.height = WINDOW_HEIGHT; //BAR_HEIGHT;
            this.x = 0;
            this.y = Screen.mainScreen.bounds.height - WINDOW_HEIGHT; //BAR_HEIGHT;

            this.timeline = new Sprite();
            this.timeline.doubleClickEnabled = true;
            this.timeline.addEventListener(MouseEvent.DOUBLE_CLICK, doubleClickHandler);
            this.stage.addChild(timeline);
            this.timeline.x = 0;
            this.timeline.y = WINDOW_HEIGHT - BAR_HEIGHT;

            remainingTime = new TextField();
            this.stage.addChild(remainingTime);

            var period:String = timeFormatter.format(sessionTimer.period);
            this.time = new TextField();
            this.time.text = period;
            this.time.setTextFormat(periodFormat);
            this.stage.addChild(this.time);
            this.time.x = Screen.mainScreen.bounds.width - time.textWidth - GUTTER;
            this.time.y = WINDOW_HEIGHT - BAR_HEIGHT;

            this.sessionTimer.addEventListener(Event.CHANGE, sessionTimer_changeHandler);
            this.sessionTimer.addEventListener(SessionTimerEvent.TIME_UP, sessionTimer_timeUpHandler);

            drawTimeline();
        }

        private function drawTimeline():void
        {
            if (sessionTimer.period <= 0)
            {
                return;
            }

            var basetimePerPeriod:Number = sessionTimer.basetime / sessionTimer.period;
            var bonusPerPeriod:Number = ((sessionTimer.remainingTime > sessionTimer.adjustedBonus) ? sessionTimer.adjustedBonus : sessionTimer.remainingTime) / sessionTimer.period;

            var bonusWidth:Number = Screen.mainScreen.bounds.width * bonusPerPeriod;
            var basetimeWidth:Number = Screen.mainScreen.bounds.width * basetimePerPeriod;
            basetimeWidth = (basetimeWidth > 0) ? basetimeWidth : 0;

            this.timeline.graphics.clear();
            this.timeline.graphics.beginGradientFill("linear", [BONUSTIME_COLOR, BONUSTIME_COLOR], [1.0, 0.8], [0, 128]);
            this.timeline.graphics.drawRoundRect(0, 0, bonusWidth, BAR_HEIGHT, BAR_ROUND, BAR_ROUND);

            this.timeline.graphics.beginGradientFill("linear", [BASETIME_COLOR, BASETIME_COLOR], [1.0, 0.8], [0, 128]);
            this.timeline.graphics.drawRoundRect(bonusWidth, 0, basetimeWidth, BAR_HEIGHT, BAR_ROUND, BAR_ROUND);
        }

        private function drawRemainingTime():void
        {
            var seconds:int = sessionTimer.remainingTime / 1000;
            if (seconds <= ALARM_TIME)
            {
                this.remainingTime.text = seconds.toString();
				this.remainingTime.setTextFormat(this.remainingTimeFormat);
				this.remainingTime.x = GUTTER;
				this.remainingTime.y = WINDOW_HEIGHT - this.remainingTime.textHeight - BAR_HEIGHT;
				this.remainingTime.width = this.remainingTime.textWidth + GUTTER;
				this.remainingTime.height = this.remainingTime.textHeight + GUTTER;
            }

            var period:String = timeFormatter.format(sessionTimer.period);
			var remainingTimeStr:String = timeFormatter.format(sessionTimer.remainingTime);
            this.time.text = remainingTimeStr + " / " + period;
            this.time.x = Screen.mainScreen.bounds.width - time.textWidth - GUTTER;
            this.time.y = WINDOW_HEIGHT - BAR_HEIGHT;
        }

        private function sessionTimer_changeHandler(event:Event):void
        {
            drawTimeline();
            drawRemainingTime();
        }

        private function doubleClickHandler(event:MouseEvent):void
        {
            // TODO 確認
            super.close();
        }

        private function sessionTimer_timeUpHandler(event:Event):void
        {
            super.close();
        }
    }
}